const app = getApp();
var t = '';//用于清除计时事件

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //---------初始化信息---------------
    orderListType:'',
    pageUrl:'',//跳转页面
    userInfo:'',//用户信息
    personOrderInfo: '',//订单信息
    pesonalCartInfo: '',//购物车数量
    customerServicePhone:'',//客服电话
    isBindPhone: '',//是否已经绑定手机
    //------用户绑定手机操作-------------
    animationData:'',//遮罩层动画
    // opMask:0,
    isshow:false,
    isPhone:false,//验证手机号是否成功
    getCode: app.data.getCode,//是否已经获取验证码
    codeTime: app.data.codeTime,//验证码有效时间
    phoneNum:'',//输入绑定的手机号
    sendCode: false,//已经触发发送验证码
    showBindPhoneModel:false,//绑定手机模态框
    
    //-----登录--------------------------
    showLogin: false,//是否展示登录
    hasShop: false,//自己是否已经拥有分销店铺
    isOwnShop: '', //是否为自己的店铺：1---自己的店铺；0--别人的店铺；null---平台
  },
  onLoad:function(){
    this.getShopInfo();//获取登录授权情况、店铺信息
    
  },
  getDiscountCard:function(){//获取优惠券
    let that = this;
    app.getDiscountCard();
    setTimeout(function(){
      that.setData({
        discountCardList: app.data.discountCardList
      })
    },500)
  },
  getShopInfo: function () {//获取登录授权情况
    let shopInfo = wx.getStorageSync('shopInfo');
    let isOwnShop = shopInfo.isOwnShop;//是否是自己的店铺
    this.setData({
      isAuthorization: app.data.isAuthorization,
      userInfo: wx.getStorageSync('userInfo'),
      isBindPhone: wx.getStorageSync('isBindPhone'),//检查用户是否绑定手机
      isOwnShop: isOwnShop || ''//用户是否拥有店铺
    })
  },
  onShow:function(){
    this.getElementsCounts();//获取订单数量
    this.getDiscountCard();//获取优惠券
  },
  getElementsCounts:function(){
    let that = this;
    let isAuthorization = that.data.isAuthorization;
    console.log('userjs:isAuthorization 授权情况' + isAuthorization);
    if (isAuthorization) {//已授权
      wx.showLoading({
        title: '加载中',
        mask: true,
        success: function (res) {
          app.getElementsCounts();//获取订单、购物车数量
          setTimeout(function () {
            that.setData({
              personOrderInfo: app.data.personOrderInfo,//用户订单信息
              pesonalCartInfo: app.data.pesonalCartInfo,//用户购物车信息
              customerServicePhone: app.data.customerServicePhone,//客服电话
            })
            wx.hideLoading();
          }, 300)
        }
      })
    } 
  },
  toShopingCart:function(e){//跳转我的购物车
    let that = this;
    wx.switchTab({
      url: '/pages/shopingCart/shopingCart',
    })
  },
  checkOrder:function (e) {
    let that = this;
    console.log(e);
    this.setData({
      orderListType: e.currentTarget.dataset.type
    });
    let listType = this.data.orderListType;
    let activeIndex = 0;//tab位置
    let statusOri = '';//订单状态
    if (listType =='all'){
      activeIndex = 0;
    } else if (listType == 'unpay') {
      activeIndex = 1;
      statusOri = '10';
    } else if (listType == 'unshipped') {
      activeIndex = 2;
      statusOri = '20';
    } else if (listType == 'shipped') {
      activeIndex = 3;
      statusOri = '30';
    } else if (listType == 'complete') {
      activeIndex = 4;
      statusOri = '50';
    }
    console.log(activeIndex);
    wx.navigateTo({
      url: '/pages/user/pages/orders/orders?activeIndex=' + activeIndex + '&&statusOri=' + statusOri,
    })
  },
  //---------------用户绑定手机号弹出框-------------------------
  bindPhone: function (e) {
    console.log('用户绑定手机号');
    // console.log(e);
    app.makeAnimation();
    this.setData({
      animationData: app.data.animationData,
      // opMask: app.data.opMask,
      showBindPhoneModel: true,
      isshow: app.data.isshow,
    })
  },
  //---------------用户注册--------------------------------
  userRegister:function(e){
    console.log(e);

    console.log("用户注册")
  },

  //关闭绑定手机模态框
  closeModel: function () {
    console.log("停止倒计时 getCode:false");
    clearTimeout(t);
    //app.cancelAnimation();
    this.setData({
      //animationData: app.data.animationData,
      
      showBindPhoneModel: false,
      isshow: false,
      getCode: false, //还未获取验证码
      sendCode: false//是否点击发送
    })
  },
  //验证手机号码
  testPhone:function(e){
    app.testPhone(e);
    this.setData({
      isPhone: app.data.isPhone,
      phoneNum: app.data.phoneNum
    })
  },
  //获取验证码
  getPhoneCode:function(e){
    let that = this;
    that.setData({
      codeTime: 60,//计时
      sendCode: true,//已经触发发送验证码(控制按钮状态)
    })
    app.getPhoneCode(e,this);
    //如果已经获取到验证码则开始倒计时
    setTimeout(function(){
      let getCode = app.data.getCode;
      console.log('是否已经发送getCode==' + getCode);
      that.setData({
        getCode: getCode,
      })
      if (getCode) {//发送验证码请求成功
        that.countTime();
      }else{//发送验证码请求失败
        that.setData({
          codeTime: 60,
          sendCode:false,
          getCode:false,
        })
      }
    },1000)
  },
  //倒计时时间
  countTime: function () {
    //let codeTime = this.data.codeTime;
    console.log("倒计时---" + this.data.codeTime);
    let [that, codeTime] = [this, this.data.codeTime]
    codeTime = codeTime - 1;
    if (codeTime == 0) {
      console.log("停止倒计时 getCode:false");
      clearTimeout(t);
      that.setData({
        getCode:false,
        sendCode:false,
        codeTime: 60 //初始化验证码过期时间
      })
      return
    } else {
      that.setData({
        codeTime: codeTime
      })
      t = setTimeout(function () {
        that.countTime();
      }, 1000)
    }

  },
 //-提交表单
  savePhoneNum:function(e){
    console.log("提交表单savePhoneNum");
    console.log(e);
    let that = this;
    let validate = app.validateSavePhoneForm(e, that);//验证表单是否合法
    if (validate) {
      app.savePhoneNum(e, that);
      setTimeout(function () {
        if (app.data.isBindPhone && !app.data.isshow) {
          clearTimeout(t);
        }
        that.setData({
          isBindPhone: app.data.isBindPhone,
          isshow: app.data.isshow,
          showBindPhoneModel: false,
          codeTime: app.data.codeTime,
          getCode: app.data.getCode
        })
      }, 1000)
    }
  },
  backToHome: function (e) {//返回到首页
    app.backToHome(this);
  },
  unOpen:function(){
    app.unOpen();
  },
  getUserObj:function(e){
    console.log(e);
    let errMsg = e.detail.errMsg;
    if (errMsg == 'getUserInfo:ok'){
       this.setData({
         userInfo: e.detail.userInfo
       })
    }
    
  },
  //复制客服微信号
  copyWxCode:function(e){
    console.log(e);
    let wxcode = e.currentTarget.dataset.wxcode;
    wx.setClipboardData({//设置系统剪贴板的内容
      data: wxcode,
      success: function (res) {
        wx.getClipboardData({//系统获取剪切板内容
          success: function (res) {
            console.log(res.data)
          }
        })
      }
    })
  },
  //跳转优惠券页
  toDiscountCardPage:function(){
    let that = this;
    wx.navigateTo({
      url: '/pages/user/discountCard/discountCard',
    })

  }
})